package inventory.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import inventory.common.CommonConstants;
import inventory.common.ResultData;
import inventory.entity.Repository;
import inventory.entity.User;
import inventory.entity.bo.QueryThingsInRepositoryCondition;
import inventory.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author sxt
 * @since 2019-11-24
 */
@Api(tags = "仓库")
@RestController
@RequestMapping(CommonConstants.REPOSITORY_PREFIX)
public class RepositoryController {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private UserService userService;
    @Autowired
    private TerminalService terminalService;
    @Autowired
    private EquipmentService equipmentService;
    @Autowired
    private MaterialStuffService materialStuffService;

    @GetMapping("getRepositoryInfoByRoleIdOrUserId/{id}")
    public ResultData getRepositoryInfoByRoleIdOrUserId(@PathVariable("id") String id) {
        List<Repository> repositories = repositoryService.selectList(
                new EntityWrapper<Repository>()
                        .eq("role_id", id)
                        .or()
                        .eq("user_id", id)
        );
        return new ResultData().OK().setObjs(repositories);
    }


    @ApiOperation("根据条件查询仓库里的东西")
    @GetMapping("getThingsInRepositoryByCondition")
    public ResultData getThingsInRepositoryByCondition(QueryThingsInRepositoryCondition queryThingsInRepositoryCondition) {
        List list;
        switch (queryThingsInRepositoryCondition.getCategoryId()) {
            case "1":
            case "2":
                list = repositoryService.selectEquipmentTerminalInRepository(queryThingsInRepositoryCondition);
                break;
            case "3":
                list = repositoryService.selectMaterialStuffRepository(queryThingsInRepositoryCondition);
                break;

            default:
                return new ResultData().FAILD().setResultIns("type类型有错");

        }
        return new ResultData().OK().setObjs(list);
    }

    @ApiOperation("根据用户workId查询仓库")
    @GetMapping("getRepositoryInfoByWorkId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tempUserId", required = true, value = "当前操作者的userId"),
            @ApiImplicitParam(name = "workId", required = true, value = "被查询者的workId")
    })
    public ResultData getRepositoryInfoByWorkId(String tempUserId, String workId) {
        //判断用户 是否 存在
        User user = userService.selectOne(new EntityWrapper<User>().eq("work_id", workId));
        User tempUser = userService.selectById(tempUserId);
        if (user == null || tempUser == null) {
            return new ResultData().FAILD().setResultIns("用户不存在");
        }

        //判断是否为同一个管理员下的用户
        boolean isSameManager = userService.isSameManager(user.getUserId(), tempUserId);
        if (!isSameManager) {
            return new ResultData().FAILD().setResultIns("用户之间没有联系");
        }

        List<Repository> repositories = repositoryService.selectRepositoryByUserId(user.getUserId());
        return new ResultData().OK().setObjs(repositories);
    }

    @RequiresPermissions("material:operate")
    @ApiOperation("根据对应的id删除具体某一个的东西")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",required = true,value = "对应的要删除东西的id"),
            @ApiImplicitParam(name = "categoryId",required = true,value = "终端1 装备2 物料3")
    })
    @GetMapping("deleteSpecificThinsById")
    public ResultData deleteSpecificThinsById(String id, String categoryId) {
        switch (categoryId) {
            case "1":
                terminalService.deleteById(id);
                break;
            case "2":
                equipmentService.deleteById(id);
                break;
            case "3":
                materialStuffService.deleteById(id);
                break;
            default:
                return new ResultData().FAILD().setResultIns("categoryId类型有错");
        }
        return new ResultData().OK();
    }


}

